using System;
using System.Security.Cryptography;
namespace MicroMsg.Common.Algorithm
{
	public class RSACS
	{
		public static string RSA_PUBLIC_KEYE = "88EB";
		public static string RSA_PUBLIC_KEYN = "A532473DA795975A50A396BE4FD0F14C9E33A4B3E68C6FD5160268475E47E5E4C9A3C8FE4AC0558532FC90AFC433BCE8C570264506FE79743FA987990BB7F98295BAFB02C71EE739D9050313436A09EB40A2957D7D2BF6172CCBD3ED874C8F29B1711C48D59DA6FF367E569709C2D94ABA081929FD4BED0F96C8D10F32F08C51";
		public static string RSA_PUBLIC_KEYD = "506E9069EEC12E8906DFE43CBD1DEE24221B935D80C45061E09A96906725794CE0FC867450AEFBFA17D9205C83E92EC1DCB967F0753CD09CA3A45199C1A501A3814098846B31266E915AC8A21A73AB7D177A7801339A58DC8507FE7A9093CD24852F12495EAF3767E64819A8570FC11493496429717C3A78B0431493D7B33BDF";
		public static int RSA_BLOCK_SIZE = 116;
		public static byte[] RSAEncrypt(byte[] DataToEncrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
		{
			byte[] result;
			try
			{
				byte[] array;
				using (RSACryptoServiceProvider rSACryptoServiceProvider = new RSACryptoServiceProvider())
				{
					rSACryptoServiceProvider.ImportParameters(RSAKeyInfo);
					array = rSACryptoServiceProvider.Encrypt(DataToEncrypt, DoOAEPPadding);
				}
				result = array;
			}
			catch (CryptographicException ex)
			{
				Console.WriteLine(ex.get_Message());
				result = null;
			}
			return result;
		}
		public static byte[] RSADecrypt(byte[] DataToDecrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
		{
			byte[] result;
			try
			{
				byte[] array;
				using (RSACryptoServiceProvider rSACryptoServiceProvider = new RSACryptoServiceProvider())
				{
					rSACryptoServiceProvider.ImportParameters(RSAKeyInfo);
					array = rSACryptoServiceProvider.Decrypt(DataToDecrypt, DoOAEPPadding);
				}
				result = array;
			}
			catch (CryptographicException ex)
			{
				Console.WriteLine(ex.ToString());
				result = null;
			}
			return result;
		}
		public static byte[] GetKeyE()
		{
			return RSACS.HexStringToByte(RSACS.RSA_PUBLIC_KEYE);
		}
		public static byte[] GetKeyN()
		{
			return RSACS.HexStringToByte(RSACS.RSA_PUBLIC_KEYN);
		}
		public static byte[] GetKeyD()
		{
			return RSACS.HexStringToByte(RSACS.RSA_PUBLIC_KEYD);
		}
		private static byte[] HexStringToByte(string hexString)
		{
			hexString = hexString.Replace(" ", "");
			if (hexString.get_Length() % 2 != 0)
			{
				hexString += " ";
			}
			byte[] array = new byte[hexString.get_Length() / 2];
			for (int i = 0; i < array.Length; i++)
			{
				array[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
			}
			return array;
		}
		public static string ByteToHexString(byte[] bytes)
		{
			string text = "";
			if (bytes != null)
			{
				for (int i = 0; i < bytes.Length; i++)
				{
					text += bytes[i].ToString("X2");
				}
			}
			return text;
		}
	}
}
